package com.bjsxt.shop.back.springmvc.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bjsxt.shop.common.controller.BaseController;
import com.bjsxt.shop.common.util.ConstatFinalUtil;
import com.bjsxt.shop.common.util.PageInfoUtil;
import com.bjsxt.shop.goods.pojo.AGoodsBrand;
import com.bjsxt.shop.goods.service.IGoodsService;
import com.bjsxt.shop.users.pojo.AAdmins;

/**
 * URL规划,拦截器拦截
 * @author WangshSxt
 *
 */
@Controller
@RequestMapping("/back/goods/")
public class GoodsBackController extends BaseController
{
	@Resource
	private IGoodsService goodsService;
	
	/**
	 * 品牌列表
	 * @return
	 */
	@RequestMapping("/brandList")
	public String brandList()
	{
		ConstatFinalUtil.SYS_LOGGER.info("---brandList--");
		return "/back/goods/brandList" ; 
	}
	
	/**
	 * 品牌列表--返回JSON数据
	 * 
	 * {
	 * 	"total":28,
	 * 	"rows":
	 * 	[
			{"productid":"AV-CB-01","productname":"Amazon Parrot","unitcost":92.00,"status":"P","listprice":63.50,"attr1":"Adult Male","itemid":"EST-18"}
		]}

	 * 
	 * @return
	 */
	@RequestMapping(value = "/brandListJSON",produces="text/html;charset=UTF-8")
	@ResponseBody
	public String brandListJSON(String page,String rows)
	{
		ConstatFinalUtil.SYS_LOGGER.info("---brandListJSON--");
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(page, rows);
		Map<String, Object> condMap = new HashMap<String, Object>();
		/* 查询列表 */
		Map<String, Object> resultMap = this.goodsService.findCondListGoodsBrandService(pageInfoUtil, condMap);
		/* 数据 */
		List<AGoodsBrand> brandList = (List<AGoodsBrand>) resultMap.get("list");
		int totalRecord = Integer.valueOf(resultMap.get("totalRecord") + "");
		
		/* 拼装数据,json */
		JSONObject resultJSON = new JSONObject() ;
		resultJSON.put("total", totalRecord);
		
		/* 数据 */
		JSONArray dataArr = new JSONArray() ; 
		int count = 1 ; 
		for (Iterator iterator = brandList.iterator(); iterator.hasNext();)
		{
			AGoodsBrand brand = (AGoodsBrand) iterator.next();
			JSONObject brandJSON = brand.toJSON();
			brandJSON.put("count", count);
			
			/* brand对象变成JSON */
			dataArr.add(brandJSON);
			count ++ ;
		}
		resultJSON.put("rows", dataArr);
		return resultJSON.toJSONString(); 
	}
	
	/**
	 * 品牌添加
	 * @return
	 */
	@RequestMapping("/brandInsert")
	public String brandInsert()
	{
		ConstatFinalUtil.SYS_LOGGER.info("---brandInsert--");
		return "/back/goods/brandInsert" ; 
	}
	
	/**
	 * 品牌添加提交
	 * @return 返回JSON
	 * {
	 * 	"code":"0",
	 * 	"info":"",
	 * }
	 */
	@RequestMapping(value = "/brandInsertSubmit",produces="text/html;charset=UTF-8")
	@ResponseBody
	public String brandInsertSubmit(String pubtimeStr,AGoodsBrand goodsBrand,HttpSession session)
	{
		ConstatFinalUtil.SYS_LOGGER.info("---brandInsertSubmit--");
		JSONObject resultJSON = new JSONObject() ; 
		
		/* 自己来转换时间,就不用转换器了 */
		goodsBrand.setPubTime(this.dateFormatUtil.strDateTime(pubtimeStr));
		
		/* 把页面上木有赋值的属性,赋值一下 */
		/* 从Session中获取 */
		AAdmins adminsSess = (AAdmins) session.getAttribute("admins");
		goodsBrand.setAdminsId(adminsSess.getId());
		goodsBrand.setCreateTime(new Date());
		goodsBrand.setUpdateTime(new Date());
		/* 保存数据 */
		resultJSON = this.goodsService.saveOneGoodsBrandService(goodsBrand);
		return resultJSON.toJSONString() ; 
	}
}
